/*  
 * Copyright (c) 2010, James Daniello
 * This project is licensed under the 'New BSD' license.
 * The full text of the license can be found in the root
 * directory under the filename "LICENSE"
 */
package src.objects;

import src.engine.PICAIULuaScript;
import src.engine.PICAIUAnimation;
import src.engine.PICAIUVector;

/**
 * The main Unit class. Has methods which are universal to all sub units, whether they are
 * gunners, rocketeers, or tanks.
 */
public abstract class PICAIUGameUnit extends PICAIUGameObject_SCD implements PICAIUCollidable, PICAIUSelectable {

  public static final int NORMAL = 0;
  public static final int GOPRONE = 1;
  public static final int PRONE = 2;
  public static final int UNPRONE = 3;
  public static final int BLOCKED = 4;
  public static final int SHOOT = 5;
  public static final int SHOOTPRONE = 6;
  public static final int RUN = 7;
  public static final int MOVE = 8;
  public static final int REST = 9;
  public static final int DEAD = 10;
  int status = NORMAL;
  //animation
  protected PICAIUAnimation animation;
  //lua script form which ai will run
  protected PICAIULuaScript script;
  //if true, red team. if false, blue team.
  protected boolean redTeam = true;
  protected boolean alive = true;
  protected boolean xFlip = false;
  //protected int def=0,off=0,healthUpgrade=0;
  protected float health, maxHealth = 100, vision = 240;
  //where the unit is moving towards
  protected PICAIUVector target = new PICAIUVector(1400, 1000);
  //target's movement
  protected PICAIUVector velocity = new PICAIUVector();
  protected float rotateSpeed = .3f;
  public static float RUNSPEED = .13f;
  public static float MOVESPEED = .08f;
  protected float moveSpeed = MOVESPEED;


  public PICAIUGameUnit() {
  }

  /**
   * Sets this objects script file as the string specified
   */
  public void loadScript(String scriptString) {
    script = new PICAIULuaScript(scriptString);
  }

  /**
   * Returns the animation for this unit when they're moving
   */
  public PICAIUAnimation getMovingAnimation() {
    return animation;
  }

  /**
   *
   */
  /**
   * Returns true if member of the red team
   */
  public boolean isRedTeam() {
    return redTeam;
  }

  /**
   * Returns true if member of the blue team
   */
  public boolean isBlueTeam() {
    return !redTeam;
  }

  public boolean isAlive() {
    return alive;
  }

  public void setAlive(boolean aliveSet) {
    alive = aliveSet;
  }

  /**
   * Returns the name of the current animation
   */
  @Override
  public String getAnimationName() {
    return animation.getAnimation();
  }

  public void setVision(int v) {
    vision = v;
  }

  /**
   * Returns the unit's vision
   */
  public float getVision() {
    return vision;
  }

  /**
   * Sets the health upgrade of this unit
   */
  //public void setHealthUpgrade(int healthUpgradeIn){
  //  healthUpgrade = healthUpgradeIn;
  //}
  /**
   * Sets the health upgrade of this unit
   */
  //public int getHealthUpgrade(){
  //  return healthUpgrade;
  //}
  /**
   * Gets the health of this unit
   */
  public float getHealth() {
    return health;
  }

  /**
   * Sets the health of this unity
   */
  public void setHealth(float healthIn) {
    health = healthIn;
  }

  /**
   * Returns the max health
   */
  public float getMaxHealth() {
    return maxHealth;
  }

  /**
   * Sets the unit's new target y location
   * @param x
   */
  public void setTargetX(int x) {
    target.setX(x);
  }

  /**
   * Returns the unit's target location x
   * @return
   */
  public int getTargetX() {
    return (int) target.getX();

  }

  /**
   * Sets the unit's new target x location
   * @param y
   */
  public void setTargetY(int y) {
    target.setY(y);
  }

  /**
   * Returns the unit's target location y
   * @return
   */
  public int getTargetY() {
    return (int) target.getY();
  }

  protected void moveTowardsTarget(long deltaT) {

    if (target.getX() < position.getX() - moveSpeed* deltaT) {
      velocity.setX(-moveSpeed* deltaT);
    } else if (target.getX() > position.getX() + moveSpeed* deltaT) {
      velocity.setX(moveSpeed* deltaT);
    } else {
      velocity.setX(0);
      status = NORMAL;
    }


    if (target.getY() < position.getY() - moveSpeed* deltaT) {
      velocity.setY(-moveSpeed* deltaT);
    } else if (target.getY() > position.getY() + moveSpeed* deltaT) {
      velocity.setY(moveSpeed* deltaT);
    } else {
      velocity.setY(0);
      status = NORMAL;
    }

    position.addToX(velocity.getX());
    position.addToY(velocity.getY());

  }

  @Override
  public int getAnimOffX() {
    return animation.getOffX();


  }

  @Override
  public int getAnimOffY() {
    return animation.getOffY();


  }

  /**
   * returns whether the current animation prohibits action;
   * @return
   */
  public boolean isBlocked() {
    return status == BLOCKED;


  }

  public boolean flip() {
    return xFlip;


  }

  /**
   *  Returns where the target cursor should be located for this object
   */
  @Override
  public int getTargetLockX() {
    return (int) position.getX() + animation.getOffX();


  }

  /**
   *  Returns where the target cursor should be located for this object
   */
  @Override
  public int getTargetLockY() {
    return (int) position.getY() + animation.getOffY();


  }

  @Override
  public void setStatus(int status) {
    this.status = status;


  }

  @Override
  public void setSpeed(float moveSpeed) {
    this.moveSpeed = moveSpeed;


  }

  @Override
  public void setTargetX(double tx) {
    target.setX(tx);



  }

  @Override
  public void setTargetY(double ty) {
    target.setY(ty);

  }

}
